package com.hmall.search.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.search.domain.dto.ItemDoc;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RequiredArgsConstructor
public class ItemListener {

    private final ItemClient itemClient;

    private final RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(
            HttpHost.create("http://192.168.188.128:9200"))
    );

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.index.queue"),
            exchange = @Exchange(name = "search.direct"),
            key = "item.index"
    ))
    public void listenerItemIndex(Long id) throws IOException {
        //获取数据库对象
        ItemDTO itemDTO = itemClient.queryItemById(id);
        if (itemDTO==null){
            return;
        }
        //转换
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        //json化
        String jsonStr = JSONUtil.toJsonStr(itemDoc);
        //准备request对象
        IndexRequest request = new IndexRequest("items").id(itemDoc.getId());
        // 准备Json文档
        request.source(jsonStr, XContentType.JSON);
        // 3.发送请求
        client.index(request, RequestOptions.DEFAULT);

        System.out.println("我检测到Rabbit给我发消息了，并存入信息"+jsonStr);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.updateStatus.queue"),
            exchange = @Exchange(name = "search.direct"),
            key = "item.updateStatus"
    ))
    public void listenerItemUpdateStatus(Long id) throws IOException {
        //获取数据库对象
        ItemDTO itemDTO = itemClient.queryItemById(id);
        if (itemDTO==null){
            return;
        }
        //转换
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        //json化
        String jsonStr = JSONUtil.toJsonStr(itemDoc);
        //准备request对象
        UpdateRequest request = new UpdateRequest("items", itemDoc.getId());
        // 准备Json文档
        request.doc(jsonStr, XContentType.JSON);
        // 3.发送请求
        client.update(request, RequestOptions.DEFAULT);

        System.out.println("我检测到Rabbit给我发消息了，并存入信息"+jsonStr);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.update.queue"),
            exchange = @Exchange(name = "search.direct"),
            key = "item.update"
    ))
    public void listenerItemUpdate(Long id) throws IOException {
        //获取数据库对象
        ItemDTO itemDTO = itemClient.queryItemById(id);
        if (itemDTO==null){
            return;
        }
        //转换
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        //json化
        String jsonStr = JSONUtil.toJsonStr(itemDoc);
        //准备request对象
        UpdateRequest request = new UpdateRequest("items", itemDoc.getId());
        // 准备Json文档
        request.doc(jsonStr, XContentType.JSON);
        // 3.发送请求
        client.update(request, RequestOptions.DEFAULT);

        System.out.println("我检测到Rabbit给我发消息了，并存入信息"+jsonStr);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.delete.queue"),
            exchange = @Exchange(name = "search.direct"),
            key = "item.delete"
    ))
    public void listenerItemDelete(Long id) throws IOException {
        //准备request对象
        DeleteRequest request = new DeleteRequest("items", id.toString());
        // 发送请求
        client.delete(request,RequestOptions.DEFAULT);

        System.out.println("我检测到Rabbit给我发消息了，并删除文档"+id);
    }




















}
